/****************************************************
	文件：PrimitiveTask.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/26 17:48:20
	功能：基元任务（框架层）
*****************************************************/

using System;
using System.Collections.Generic;

namespace AIFramework.HTN
{
    public class PrimitiveTask : TaskBase
    {
        /// <summary>
        /// 前置条件
        /// </summary>
        public Func<WorldState, bool> CheckCond;
        /// <summary>
        /// 原子行为
        /// </summary>
        protected List<BevBase> mBevs;
        /// <summary>
        /// 施加对世界状态的影响
        /// </summary>
        public Action<WorldState> ApplyEffects;
        /// <summary>
        /// 期望影响（只在规划、检查阶段对世界状态产生影响）
        /// </summary>
        public Action<WorldState> ApplyExpectedEffects;

        public PrimitiveTask(string name, TaskType taskType) : base(name, taskType)
        {
            mBevs = new List<BevBase>();
            CheckCond = (ws) => true;
            ApplyEffects = (ws) => { };
            ApplyExpectedEffects = (ws) => { };
        }

        public virtual void Reset()
        {
            mBevs.Clear();
            CheckCond = (ws) => true;
            ApplyEffects = (ws) => { };
            ApplyExpectedEffects = (ws) => { };
        }
        /// <summary>
        /// 添加前置条件s
        /// </summary>
        /// <param name="cond">前置条件委托</param>
        /// <returns></returns>
        public PrimitiveTask AddCond(Func<WorldState, bool> cond)
        {
            CheckCond = cond;
            return this;
        }
        /// <summary>
        /// 添加原子行为
        /// </summary>
        /// <param name="bevs">原子行为集合</param>
        public PrimitiveTask AddBev(BevBase bev)
        {
            mBevs.Add(bev);
            return this;
        }
        /// <summary>
        /// 添加 对世界状态施加影响 的委托
        /// </summary>
        /// <param name="applyEffects">对世界状态施加影响的委托</param>
        /// <returns></returns>
        public PrimitiveTask AddEffects(Action<WorldState> applyEffects)
        {
            ApplyEffects = applyEffects;
            return this;
        }
        /// <summary>
        /// 添加 对世界状态施加影响 的委托：期望影响（只在规划、检查阶段对世界状态产生影响）
        /// </summary>
        /// <param name="applyExpectedEffects">期望影响</param>
        /// <returns></returns>
        public PrimitiveTask AddExpectedEffects(Action<WorldState> applyExpectedEffects)
        {
            ApplyExpectedEffects = applyExpectedEffects;
            return this;
        }
        /// <summary>
        /// 执行该任务下的 原子行为
        /// </summary>
        public void Execute()
        {
            foreach (var bev in mBevs)
            {
                bev.Execute();
            }
        }
    }
}
